<?php
/**
 * mysql嘞
 */
#$header("Content-type: text/html; charset=utf-8");
ini_set('memory_limit', '2000M');
$dbname = $argv[1];
#$dir='./sh/sh_20160621/house_sell_bad/';
$dir= $argv[2];
echo $dir."\n";
#list($city, $date, $tbl) = explode("/", $dir);
$online_tbl = basename($dir);
$tbl = basename($dir)."_new";
$oldtbl = basename($dir);
echo $tbl."\n";
#die;
Class washData{
    // 数据库对象
    private $db;
    private $startTime;
    public $dbname;
    // 数据库配置
    private $dbconf = array(
        'host' => 'rds1tu038044m8zub4o7.mysql.rds.aliyuncs.com',
        'user' => 'zhugef_online',
        'post' => '3306',
        'pass' => 'emd3ZW56*$FuYmFanVuZmVuZw%db',
        'dbname' => '',
        'code' => 'utf8'
    );

    
    public function __construct($config = null){
        if(!empty($config)){
                $this->dbconf['dbname'] = $config;
        }
	var_dump($this->dbconf);
        $this->db = mysql_pconnect($this->dbconf['host'].':'.$this->dbconf['post'], $this->dbconf['user'], $this->dbconf['pass']) or die('数据库服务器连接错误:' . mysql_error());
        mysql_select_db($this->dbconf['dbname']);
        mysql_query("set names '".$this->dbconf['code']."'");
        $this->startTime = time();
}
    public function query_sql($sql = ''){
 	if(!empty($sql)){
        #echo 'sql='.$sql."\n";
		return mysql_query($sql);
	}
	return false;
    }

}

#$washData = new washData($dbname);

//$path = './part-00005';
getSqlList($dir, $tbl, $oldtbl, $dbname);
mysql_close();
function getSqlList($dir, $tbl, $oldtbl,$dbname){
    $washData = new washData($dbname);
    $sql = "drop table ".$tbl;
    $washData->query_sql($sql);
    $sql = "create table ".$tbl." like ".$oldtbl;
    echo $sql;
    $washData->query_sql($sql);
    
    $filelist = getFiles($dir);
    var_dump($filelist);
    foreach((array)$filelist as $key => $value){
        if(isExits($value, 'part-')){
	    $value = $dir.$value;
	    echo $value."\n";
	    $content = file_get_contents($value);
            $sql = explode("INSERT INTO ", $content);
            unset($sql[0]);
	    foreach((array)$sql as $v){
            #try{
	        $response = $washData->query_sql('INSERT INTO '.$v);
            echo mysql_errno() . ": " . mysql_error() . " ";
            #echo 'response='.$response."\n";
            #}
            #catch (\Exception $e) {
            if ($response== False){
                sleep(10);
                #echo 'error';
                $washData = new washData($dbname);
                sleep(1);
                $response = $washData->query_sql('INSERT INTO '.$v);
                echo 'response='.$response."\n";
            }
                

	    }
          //return;
	}
    }
}
function getFiles($dir){
    $handler = opendir($dir);
    while(($file = readdir($handler)) !== false){
        if($file == "."||$file == ".."||$file == '.DS_Store'){continue;}
//        if (!is_dir($file)) {
            $data[] = $file;
//        }
    }
    return $data;
}

function isExits($str, $ser){
     $temp = str_replace($ser, '', $str);
     return $str != $temp;
}
